package org.lanqiao.tjut.control;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import org.lanqiao.tjut.bean.ContactInfoBean;
import org.lanqiao.tjut.bean.UsersEntity;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserLoginControl {
	// 组合service层，依赖注入
	@Resource
	private IUserInfoService userService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(String userName, String userAge, Model model) {
		System.out.println(userName + "正在进行登录处理。。。");
		UsersEntity userE = userService.selectUsersInfo(2);
		String strName = (userE == null ? "" : userE.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addAttribute("loggerName", strName);
		return "logined";
	}

	@RequestMapping("/login2")
	public ModelAndView doLogin2(UsersEntity userE) {
		System.out.println("从表单获取的值为：name-" + userE.getUsername());
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		UsersEntity userE0 = userService.selectUsersInfo(2);
		String strName = (userE0 == null ? "" : userE0.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addObject("loggerName", strName);
		return model;
	}

	// 组合
	@RequestMapping("/login3")
	public ModelAndView doLogin3(UsersEntity userE) {
		System.out.println("从表单获取的值为：name-" + userE.getContactInfoB().getTelphone());
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		UsersEntity userE0 = userService.selectUsersInfo(2);
		String strName = (userE0 == null ? "" : userE0.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addObject("loggerName", strName);
		return model;
	}

	// list
	@RequestMapping("/login4")
	public ModelAndView doLogin4(UsersEntity userE) {
		// 解析表单的值
		List<ContactInfoBean> lstC = userE.getLstContactInfoB();
		String strPagePamaName = "没有获取到值1。";
		if (lstC != null && lstC.size() > 0) {
			strPagePamaName = lstC.get(0).getTelphone();
		}
		System.out.println("从表单获取的值为：name1-" + strPagePamaName);
		// 解析第二个元素的值
		String strPagePamaName2 = "没有获取到值2。";
		if (lstC != null && lstC.size() > 1) {
			strPagePamaName2 = lstC.get(1).getTelphone();
		}
		System.out.println("从表单获取的值为：name2-" + strPagePamaName2);

		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		UsersEntity userE0 = userService.selectUsersInfo(2);
		String strName = (userE0 == null ? "" : userE0.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addObject("loggerName", strName);
		return model;
	}

	// map
	@RequestMapping("/login5")
	public ModelAndView doLogin5(UsersEntity userE) {
		// 解析表单的值
		Map<String, ContactInfoBean> mapC = userE.getMapContactInfoB();
		String strPagePamaName = "没有获取到值1。";
		// 解析第二个元素的值
		String strPagePamaName2 = "没有获取到值2。";
		if (mapC != null && mapC.size() > 0) {
			strPagePamaName = mapC.get("contact0") == null ? "" : mapC.get("contact0").getTelphone();
			strPagePamaName2 = mapC.get("contact1") == null ? "" : mapC.get("contact1").getTelphone();
		}
		System.out.println("从表单获取的值为：name1-" + strPagePamaName);
		System.out.println("从表单获取的值为：name2-" + strPagePamaName2);

		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		UsersEntity userE0 = userService.selectUsersInfo(2);
		String strName = (userE0 == null ? "" : userE0.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addObject("loggerName", strName);
		return model;
	}

	@RequestMapping("/login6")
	public ModelAndView doLogin6(UsersEntity userE) {
		// 解析表单的值
		Set<ContactInfoBean> setC = userE.getSetContactInfoB();
		if (setC != null && setC.size() > 0) {
			for (ContactInfoBean conB : setC) {
				System.out.println("从表单获取的值为：tel-" + conB == null ? "null" : conB.getTelphone());
			}
		}

		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		UsersEntity userE0 = userService.selectUsersInfo(2);
		String strName = (userE0 == null ? "" : userE0.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addObject("loggerName", strName);
		return model;
	}

	@RequestMapping(value = "/login7", method = RequestMethod.POST)
	public String doLogin7(String userName, @RequestParam(value = "userage") String userAge, Model model) {
		System.out.println("表单userName：=" + userName);
		System.out.println("表单userage：=" + userAge);
		UsersEntity userE = userService.selectUsersInfo(2);
		String strName = (userE == null ? "" : userE.getUsername());
		System.out.println("数据库查询的用户信息为：" + strName);
		model.addAttribute("loggerName", strName);
		return "logined";
	}

	@RequestMapping("/login8")
	// @ModelAttribute("")
	public String doLogin8(Model model) {
		UsersEntity user = new UsersEntity();
		user.setUsername("小明");
		user.setUserage(20);
		model.addAttribute("userE", user);
		return "logined";
	}

	@RequestMapping("/login9")
	public String doLogin9(@ModelAttribute("userE") UsersEntity userE) {
		System.err.println("suerName:" + userE == null ? "k" : userE.getUsername());
		return "test";
	}
}
